var obox = document.querySelector(".Carousel_figure")
var leftBtn = document.querySelector(".leftBtn")
var rightBtn = document.querySelector(".rightBtn")
var closeBtn = document.querySelector(".close")
var ware = document.querySelectorAll(".warehouse_img")
var photo = document.querySelectorAll(".photo_space>img")
for (var i = 0; i < ware.length; i++) {
    ware[i].index = i
    ware[i].addEventListener("click", figure, false)
}
leftBtn.addEventListener("click", left, false)
rightBtn.addEventListener("click", right, false)
closeBtn.addEventListener("click", function () {
    obox.style.display = "none"
})


function figure() {
    var k = this.index
    obox.style.display = "block"
    for (var j = 0; j < photo.length; j++) {
        add(photo[j], "photo_img3")
    }
    add(photo[k], "photo_img1")
    if (k == 0) {
        add(photo[3], "photo_img")
        add(photo[1], "photo_img2")
    } else if (k == 3) {
        add(photo[2], "photo_img")
        add(photo[0], "photo_img2")
    } else {
        add(photo[k - 1], "photo_img")
        add(photo[k + 1], "photo_img2")
    }
}

function left() {
    var fa=document.querySelector(".photo_space")
    var obj = remove(fa.children[3])
    fa.insertBefore(obj,fa.children[0])
    var nowthis = document.querySelector(".photo_img1")
    var pre = nowthis.previousElementSibling
    var pre2 = pre.previousElementSibling
    for (var i = 0; i < photo.length; i++) {
        add(photo[i], "photo_img3")
    }
    
    add(pre, "photo_img1")
    add(nowthis,"photo_img2")
    add(pre2,"photo_img")
}
function right() {
    var fa=document.querySelector(".photo_space")
    var obj = remove(fa.children[0])
    fa.appendChild(obj)
    var nowthis = document.querySelector(".photo_img1")
    var next = nowthis.nextElementSibling
    var next2 = next.nextElementSibling
    for (var i = 0; i < photo.length; i++) {
        add(photo[i], "photo_img3")
    }
    
    add(next, "photo_img1")
    add(nowthis,"photo_img")
    add(next2,"photo_img2")
}

function add(ev, classname) {
    ev.className = " "
    ev.classList.add(classname)
}


function remove(e){
    var _e = e.parentNode.removeChild(e);
    return _e;
}